package com.vma.shoeSystem.controller;

import com.jfinal.core.Controller;
import com.jfinal.kit.PathKit;
import com.jfinal.upload.UploadFile;
import com.vma.shoeSystem.util.GetPathByDate;
import com.vma.shoeSystem.util.ReturnsUtil;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Created by chenzui on 2017/3/28.
 */
public class FileController extends Controller {
    /**
     * upload 图片上传方法
     * @throws IOException
     */
    public void upload() throws IOException {
        try{
            UploadFile file = getFile();
            String imgName = file.getFileName();

            File source = new File(PathKit.getWebRootPath() + "/temp/" + imgName); // 获取临时文件对象
//            String savePath = PathKit.getWebRootPath() + "/" + GetPathByDate.getPath();

            String savePath = "/alidata01/chenz/www/Static_war_exploded" + "/" + GetPathByDate.getPath();
            FileInputStream fis = new FileInputStream(source);
//
            File targetDir = new File(savePath);
            if (!targetDir.exists()) {
                targetDir.mkdirs();
            }

            File target = new File(targetDir, imgName);
            if (!target.exists()) {
                target.createNewFile();
            }

            FileOutputStream fos = new FileOutputStream(target);
            byte[] bts = new byte[1024 * 20];
            while (fis.read(bts, 0, 1024 * 20) != -1) {
                fos.write(bts, 0, 1024 * 20);
            }

            fos.close();
            fis.close();
            renderJson(ReturnsUtil.returnSuccess( "/"+GetPathByDate.getPath()+"/"+imgName));
        }catch (Exception e){
            e.printStackTrace();
            renderJson(ReturnsUtil.returnException(e.getMessage(),e ));

        }

    }


    public void uploadImg() throws Exception {
        UploadFile uploadFile = getFile("uploadFile", PathKit.getWebRootPath()
                + "/temp", 20 * 1024 * 1024, "utf-8"); // 最大上传20M的图片
        // 异步上传时，无法通过uploadFile.getFileName()获取文件名
        String fileName = getPara("fileName");
        fileName = fileName.substring(fileName.lastIndexOf("\\") + 1); // 去掉路径

        // 异步上传时，无法通过File source = uploadFile.getFile();获取文件
        File source = new File(PathKit.getWebRootPath() + "/temp/" + fileName); // 获取临时文件对象

        String extension = fileName.substring(fileName.lastIndexOf("."));
        String savePath = PathKit.getWebRootPath() + "/upload/images/"
                + GetPathByDate.getPath();
        FileInputStream fis = new FileInputStream(source);

        File targetDir = new File(savePath);
        if (!targetDir.exists()) {
            targetDir.mkdirs();
        }

        File target = new File(targetDir, fileName);
        if (!target.exists()) {
            target.createNewFile();
        }

        FileOutputStream fos = new FileOutputStream(target);
        byte[] bts = new byte[1024 * 20];
        while (fis.read(bts, 0, 1024 * 20) != -1) {
            fos.write(bts, 0, 1024 * 20);
        }

        fos.close();
        fis.close();
    }
}
